package dtd.PHS.VND_ExchangeRate.widget;

import java.util.ArrayList;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.VNExActivity;
import dtd.PHS.VND_ExchangeRate.data.DataProvider;
import dtd.PHS.VND_ExchangeRate.data.IDelegateDataGetter;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.data.RateItem;
import dtd.PHS.VND_ExchangeRate.data.SJCItem;
import dtd.PHS.VND_ExchangeRate.data.VNExDatabase;
import dtd.PHS.VND_ExchangeRate.data.RateItem.MyCurrency;
import dtd.PHS.VND_ExchangeRate.utils.Logger;

/**
 * Precondtion: AppWidgetManager.EXTRA_APPWIDGET_ID must always be set before come to this activity:
 * 	1. From system picker (android)
 * 	2. From Click on the widget (me)
 * @author Hung Son
 *
 */
public class ChooseWidgetCurrency extends VNExActivity implements IDelegateDataGetter {

	private ListView list;
	private int appWidgetId;
	private ListRates listrates;
	private ArrayAdapter<String> adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.choose_widget_currency);
	    extractWidgetId();
	    createViews();

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				VNExDatabase.insertWidgetCurrency(appWidgetId,adapter.getItem(position));
				DataProvider.getInstance().requestListRates(ChooseWidgetCurrency.this);
			}
		});

	}

	private void createViews() {
		list = (ListView) findViewById(R.id.list);
		adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getAllRateNGold());
		list.setAdapter(adapter);
	}

	private void extractWidgetId() {
		Intent src = getIntent();
		appWidgetId = src.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
		if ( appWidgetId == -1) {
			Logger.logError(" No appwidget id !");
			finish();
		}
	}

	private String[] getAllRateNGold() {
		MyCurrency[] values = MyCurrency.values();
		String[] ret = new String[values.length-1];
//		ret[0] = SJCItem.SJC;
		int i = 0;
		for(MyCurrency c : values) {
			if ( c != MyCurrency.VND)
				ret[i++] = c.toString();
		}
		return ret;
	}

	@Override
	public void onGetDataCompleted(Object data) {
		if ( data instanceof ListRates) {
			listrates = (ListRates) data;
			updateWidgets();
			Intent resultValues = new Intent();
			resultValues.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
			setResult(RESULT_OK, resultValues);
			finish();

		}
	}

	private void updateWidgets() {
		ArrayList<WidgetItem> listInfo = VNExDatabase.getAllWidgetInfo();
		for(WidgetItem item : listInfo ) {
			updateWidget( item );
		}
	}

	private void updateWidget(WidgetItem item) {
		int id = item.getId();
		String currency = item.getCurrency();		
		if ( currency.equals(SJCItem.SJC)) {
			
		} else {
			RateItem rate = null;
			for(RateItem rateItem : listrates) {
				if ( rateItem.getCurrencyName().equals(currency)) {
					rate = rateItem;
					break;
				}
			}
			updateCurrencyWidget(getApplicationContext(),id,rate);
		}
	}

	static public  void updateCurrencyWidget(Context context, int id, RateItem rate) {
		RateRemoteView rateRemoteView = new RateRemoteView(context);
		rateRemoteView.setRate(rate);
		try {
			AppWidgetManager.getInstance(context).updateAppWidget(id, rateRemoteView);
		} catch (Exception e) {
			Logger.logError(e);
		}
	}


	@Override
	public void onGetDataError(Exception e) {
	}

}
